package com.carbon.assets.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.assets.service.CarbonExchangeService;
import com.carbon.assets.param.CarbonExchangeQueryParam;
import com.carbon.assets.entity.CarbonExchange;
import com.carbon.assets.common.BaseController;
import com.carbon.assets.vo.CarbonExchangeAccountVo;
import com.carbon.assets.vo.CarbonExchangeReturnVo;
import com.carbon.domain.common.ApiResult;
import com.carbon.trade.entity.CarbonTradeQuote;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 碳交易所  前端控制器
 * </p>
 *
 * @author Li Jun
 * @since 2021-08-16
 */
@Slf4j
@RestController
@RequestMapping("/carbonExchange")
@Api(value = "碳交易所 模块", tags = {"碳交易所 模块"})
public class CarbonExchangeController extends BaseController {
    @Autowired
    CarbonExchangeService carbonExchangeService;
    @PostMapping("/getListByTenant")
    public ApiResult<List<CarbonExchange>> getListByTenant(@RequestBody CarbonExchangeQueryParam param) {
        // 调用 Service 层方法获取数据并返回给前端
        List<CarbonExchange> listByTenant = carbonExchangeService.getListByTenant(param);
        return ApiResult.ok(listByTenant);
    }
    @PostMapping("/getListByTenantB")
    public ApiResult<CarbonExchangeReturnVo> handleSearch(@RequestBody CarbonExchangeQueryParam param) {
        Page<CarbonExchange> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<CarbonExchange> paging = carbonExchangeService.handleSearchB(page,param);
        CarbonExchangeReturnVo carbonExchangeAccountVo = new CarbonExchangeReturnVo();
        carbonExchangeAccountVo.setTotal((int) paging.getTotal());
        carbonExchangeAccountVo.setList(paging.getRecords());
        return ApiResult.ok(carbonExchangeAccountVo);
    }
    //update
    @PostMapping("/update")
    public ApiResult<Boolean> update(@RequestBody CarbonExchange carbonExchange) {
        return ApiResult.result(carbonExchangeService.updateById(carbonExchange));
    }
    @PostMapping("/updateStatus")
    public ApiResult updateStatus(@RequestBody Map<String, Object> params) {
        try {
            // 从请求参数中获取id和status
            Long id = Long.valueOf(params.get("id").toString());
            Integer status = Integer.valueOf(params.get("status").toString());

            // 调用服务层方法更新状态
            boolean success = carbonExchangeService.updateStatus(id, status);

            if (success) {
                return ApiResult.ok("状态更新成功");
            } else {
                return ApiResult.fail("状态更新失败");
            }
        } catch (NumberFormatException e) {
            return ApiResult.fail("参数格式错误");
        } catch (Exception e) {
            // 实际项目中建议记录日志
            e.printStackTrace();
            return ApiResult.fail("服务器内部错误");
        }
    }
    @PostMapping("/delete")
    public ApiResult<Boolean> delete( @RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        return ApiResult.result(carbonExchangeService.removeById(id));
    }
}

